<template>
	<view class="used-car-wrap">
		<view class="item" v-for="(item, index) in usedCarList" :key="index">
			<navigator :url="`/pages/used-car/infoPage?car_id=${item.id}`" class="clear link">
				<image class="img pull-left" mode="aspectFill" :src="`${host}/${item.p_image}`"></image>
				<view class="info-wrap pull-left">
						<view class="name u-line-1">
							{{item.carname}}
						</view>
						<view class="mileage">
							{{item.card_time}} / {{item.mileage}}
						</view>
						<view class="price">
							{{item.price}} 万
						</view>
					<view class="btn-wrap flex-x-between">
						<view class="btn-item revoke text-center" @click.stop="handleRevoke(item.id)">
							撤销
						</view>
						<view v-if="item.times > 0" class="btn-item update text-center">
							<u-count-down @end="item.times = 0" :timestamp="item.times" :show-days="false" bg-color="transparent" font-size="24rpx" separator-size="24rpx" color="#FFFFFF" separator-color="#FFFFFF"></u-count-down>
						</view>
						<view v-else class="btn-item update text-center" @click.stop="handleUpdate(item.id)">
							<view>更新</view>
						</view>
					</view>
				</view>
			</navigator>
			<view class="time-group">
				<view class="time-group__item">
					<view class="time-group__name">
						发布时间：
					</view>
					<view class="time-group__time">
						{{item.create_time}}
					</view>
				</view>
				<view class="time-group__item">
					<view class="time-group__name">
						更新时间：
					</view>
					<view class="time-group__time">
						{{item.sort_time}}
					</view>
				</view>
				<view class="time-group__item">
					<view class="time-group__name">
						审核时间：
					</view>
					<view class="time-group__time" :class="{'time-group__time--light':item.switch_time === '待审核'}">
						{{item.switch_time}}
					</view>
				</view>
			</view>
		</view>
		<view class="update-wrap" v-if="showUpdate">
			<view class="mask"></view>
			<view class="form-wrap">
				<view class="title text-center">
					更新
				</view>
				<view class="form-item">
					<input type="digit" placeholder-style="color:#9A9A9A" v-model="form.price" placeholder="请输入更新后的价格(万元)" class="input text-center w100">
				</view>
				<view class="btn-wrap relative">
					<text class="cancel-btn btn inline-block text-center" @click="handleUpdate">取消</text>
					<text class="confirm-btn btn inline-block text-center" @click="handleConfirm">确定</text>
				</view>
			</view>
		</view>
		<no-data v-if="!usedCarList.length" texts="您还没有卖车信息" />
	</view>
</template>

<script>
	import {
		getUsedCar,
		updateUsedCar,
		cancelUsedCar
	} from '../../../apis/personal/index.js'

	export default {
		data() {
			return {
				id: 0,
				showUpdate: false,
				form: {
					price: ''
				},
				rules: {
					price: [{
						required: true,
						message: '请输入更新后的价格(万元)',
					}]
				},
				usedCarList: []
			}
		},
		computed: {
			host() {
				return this.$baseHost.qiniuHost;
			}
		},
		onShow() {
			this.getUsedCarList()
		},
		methods: {
			handleUpdate(id) {
				id && (this.id = id)
				this.showUpdate = !this.showUpdate
			},
			handleRevoke(car_id) {
				uni.showModal({
					title: '提示',
					content: '你确定要撤销吗？撤销之后再发起需要平台重新审核',
					success: res => {
						if (res.confirm) {
							const params = {
								car_id,
								uid: uni.getStorageSync("userId")
							}
							this.$loading.show()
							cancelUsedCar(params).then(res => {
								this.$loading.hide()
								this.getUsedCarList()
							})
						}
					}
				})
			},
			handleConfirm() {
				const params = {
					car_id: this.id,
					new_price: this.form.price,
					uid: uni.getStorageSync("userId")
				}

				this.$loading.show()
				this.$verification(this.form, this.rules, ok => {
					if (ok) {
						updateUsedCar(params).then(res => {
							this.$loading.hide()
							this.handleUpdate()
							this.resetForm()
							this.getUsedCarList()
						})
					}
				})
			},
			resetForm() {
				this.form.price = ''
			},
			getUsedCarList() {
				const params = {
					uid: uni.getStorageSync("userId")
				}
				this.$loading.show()
				getUsedCar(params).then(res => {
					this.$loading.hide()
					this.usedCarList = res.data.list
				})
			}
		}
	}
</script>

<style lang="less">
	.used-car-wrap {
		padding: 30rpx;
		
		.item {
			padding: 20rpx;
			box-shadow: 0rpx 4rpx 15rpx 0rpx rgba(0, 0, 0, 0.1);
			border-radius: 10rpx;
			margin-bottom: 20rpx;

			.link{
				display: flex;
			}
			.img {
				width: 215rpx;
				height: 215rpx;
				border-radius: 10rpx;
			}
	
			.info-wrap {
				flex: 1;
				margin-left: 20rpx;
				position: relative;
				overflow: hidden;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				.name {
					font-size: 34rpx;
				}

				.mileage{
					font-size: 26rpx;
					color: #343434;
				}
				
				.price{
					color: #E41906;
					font-size: 32rpx;
					font-weight: 500;
				}
				
				.amount-wrap {
					font-size: 26rpx;
					.amount {
						font-size: 30rpx;
						color: #E41805;
					}
				}
			}

			.btn-wrap {
				margin-top: 20rpx;

				.btn-item {
					width: 200rpx;
					height: 48rpx;
					font-size: 24rpx;
					line-height: 48rpx;
					border: 1rpx solid #E41805;
					color: #E41805;
					border-radius: 30rpx;

					&.update {
						background: #E41805;
						color: #ffffff;
						display: flex;
						align-items: center;
						justify-content: center;
					}
				}
			}
		}

		.update-wrap {
			position: fixed;
			width: 100%;
			top: 0;
			height: 100%;

			.mask {
				position: relative;
				background: rgba(0, 0, 0, .2);
				width: 100%;
				height: 100%;
			}

			.form-wrap {
				position: absolute;
				top: 50%;
				left: 46%;
				border-radius: 20rpx;
				background: #ffffff;
				transform: translate(-50%, -50%);
				width: 90%;

				.title {
					padding: 40rpx 0rpx;
					color: #343434;
					font-weight: bold;
				}

				.form-item {
					margin-bottom: 50rpx;
					background: #f8f8f8;

					.input {
						font-size: 30rpx;
						padding: 25rpx 0rpx;
					}
				}

				.btn-wrap {
					padding: 20rpx 0rpx;
					border-top: 1rpx solid rgba(202, 201, 201, .5);

					&::after {
						position: absolute;
						top: 0;
						left: 50%;
						transform: translate(-50%, 0);
						content: '';
						display: inline-block;
						width: 1rpx;
						height: 100%;
						background: rgba(202, 201, 201, .5);
					}

					.btn {
						width: 50%;
					}
					.confirm-btn{
						color: #E41906;
					}
				}
			}
		}
	}
	
	.time-group{
		display: flex;
		margin-top: 30rpx;
		padding-top: 20rpx;
		box-shadow: 0 -1rpx 0.5rpx -1rpx #CCCCCC;
		&__item{
			flex: 1;
			font-size: 22rpx;
			color: #989A9A;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
		}
		&__time{
			&--light{
				color: #F9BC28;
			}
		}
	}
</style>
